package com.xxr.mom.ci.charging.interworking.v2016.accepter.query;

import cn.hutool.core.collection.CollectionUtil;
import com.xxr.mom.ci.charging.device.entity.Gun;
import com.xxr.mom.ci.charging.device.entity.Station;
import com.xxr.mom.ci.charging.interworking.base.InterfaceAccepter;
import com.xxr.mom.ci.charging.interworking.dto.InterworkingConfigDto;
import com.xxr.mom.ci.charging.interworking.v2016.AbstractV2016Service;
import com.xxr.mom.ci.charging.interworking.vo.ConnectorStatusInfo;
import com.xxr.mom.ci.charging.interworking.vo.StationStatusInfo;
import com.xxr.mom.ci.charging.interworking.vo.api.QueryStationStatusResponse;
import com.xxr.mom.ci.charging.mapper.device.GunMapper;
import com.xxr.mom.ci.charging.mapper.device.StationMapper;
import com.xxr.mom.ci.common.enums.InterworkingRetEnum;
import com.xxr.mom.ci.common.exception.InterworkingException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 设备接口状态查询
 * 概述：此接口用于批量查询设备实时状态
 * 接口名称：query_station_status
 * 接口使用方法：由基础设施运营商方实现此接口，数据需求方调用
 *
 * @description: QueryStationStatus
 * @date: 2024/8/10 23:33
 * @author: natsuki_kining
 * @version: 1.0
 */
@Log4j2
@Service
public class QueryStationStatusAccepterService extends AbstractV2016Service<List<String>, QueryStationStatusResponse> implements InterfaceAccepter<List<String>, QueryStationStatusResponse> {

    @Autowired
    private StationMapper stationMapper;
    @Autowired
    private GunMapper gunMapper;

    @Override
    public void validateRequestParam(List<String> decodeRequestParamList) {
        // 最大查询个数
        int queryStationMaxSize = 50;
        if (CollectionUtil.isEmpty(decodeRequestParamList) || decodeRequestParamList.size() > queryStationMaxSize) {
            throw new InterworkingException(InterworkingRetEnum.BUSINESS_PARAMETER_ERROR);
        }
    }

    @Override
    public QueryStationStatusResponse dataHandler(List<String> stationNumbers, InterworkingConfigDto configDto) {
        QueryStationStatusResponse stationStatusInfos = new QueryStationStatusResponse();
        List<StationStatusInfo> stationStatusInfoList = new ArrayList<>();
        List<Station> stationList = stationMapper.selectByNumbers(stationNumbers);
        if (CollectionUtil.isEmpty(stationList)) {
            return stationStatusInfos;
        }
        stationList.forEach(station -> {
            StationStatusInfo stationStatusInfo = new StationStatusInfo();
            stationStatusInfo.setStationId(station.getStationNumber());
            List<ConnectorStatusInfo> connectorStatusInfos = new ArrayList<>();
            List<Gun> gunList = gunMapper.selectByStationId(station.getId());
            if (CollectionUtil.isNotEmpty(gunList)) {
                gunList.forEach(gun -> {
                    ConnectorStatusInfo connectorStatusInfo = new ConnectorStatusInfo();
                    connectorStatusInfo.setConnectorId(gun.getConnectorId());
                    // TODO 查询枪实时状态
                    connectorStatusInfo.setStatus(1);
                    connectorStatusInfos.add(connectorStatusInfo);
                });
            }
            stationStatusInfo.setConnectorStatusInfos(connectorStatusInfos);
            stationStatusInfoList.add(stationStatusInfo);
        });
        stationStatusInfos.setTotal(stationList.size());
        stationStatusInfos.setStationStatusInfos(stationStatusInfoList);
        return stationStatusInfos;
    }
}
